//
//  HHPuckerList1ViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/10.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHPuckerList1ViewController.h"
#import "Pucker1Cell.h"

#define kCell_Height 44
@interface HHPuckerList1ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView *TableView;
    NSMutableArray *soucreDada; //TableView数据源
    NSMutableArray *sectionArray; //分区的数据
    NSMutableArray *stateArray;//状态的数据
}

@end
static NSString *const pucker1ID = @"pucker1ID";
@implementation HHPuckerList1ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView]; //设置TableView
    
    [self loadData]; //加载数据
}

#pragma mark - 加载数据
- (void)loadData{
    
    sectionArray = [NSMutableArray arrayWithObjects:@"我是小组1",@"我是小组2",@" 我是小组3",@"我是小组4",nil];
    NSArray *one = @[@"我是小组1中的cell1",@"我是小组1中的cell2",@"我是小组1中的cell3",@"我是小组1中的cell4"];
    NSArray *two = @[@"我是小组2中的cell1",@"我是小组2中的cell2",@"我是小组2中的cell3",@"我是小组2中的cell4"];
    NSArray *three= @[@"我是小组3中的cell1",@"我是小组3中的cell2",@"我是小组3中的cell3",@"我是小组3中的cell4"];
    NSArray *four = @[@"我是小组4中的cell1",@"我是小组4中的cell2",@"我是小组4中的cell3",@"我是小组4中的cell4"];
    
    soucreDada = [NSMutableArray arrayWithObjects:one,two,three,four, nil];
    
    stateArray =[NSMutableArray array];
    
    for (int i = 0; i<soucreDada.count; i++) {
        
        [stateArray addObject:@"0"];////所有的分区都是闭合
    }
}
#pragma mark - 设置TableView
- (void)setupTableView{
    self.title = @"折叠";
    TableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    TableView.delegate  = self;
    TableView.dataSource = self;
    TableView.rowHeight = 60;
    TableView.tableFooterView = [UIView new];
    [TableView registerNib:[UINib nibWithNibName:(NSStringFromClass([Pucker1Cell class])) bundle:nil] forCellReuseIdentifier:pucker1ID];
    [self.view addSubview:TableView];
    
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
//TableView一共有多少分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return soucreDada.count;
}
//每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([stateArray[section] isEqualToString:@"1"] ) {//如果是展开的状态
        NSArray *arr = soucreDada[section];
        return arr.count;
    }else{
        
        return 0;//如果是闭合
    }
}
//绘制每一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Pucker1Cell  *cell = [tableView dequeueReusableCellWithIdentifier:pucker1ID];
    cell.name.text = soucreDada[indexPath.section][indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    cell.contentView.backgroundColor = [UIColor whiteColor];
    
    return cell;
    
}
//// section上header的名字
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
//    return sectionArray[section];
//}
//自定义section的显示内容，自定义的话 则上面既没有意义了
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [button setTag:section+1];
    [button addTarget:self action:@selector(sectionbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
    line.image =[UIImage imageNamed:@"line_real"];
    [button addSubview:line];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-22)/2, 22, 22)];
    imgView.image = [UIImage imageNamed:@"ico_faq_d"];
    [button addSubview:imgView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(40,(44-22)/2 , 200, 22)];
    label.backgroundColor = [UIColor greenColor];
    label.font = [UIFont systemFontOfSize:15];
    label.text  = sectionArray[section];
    [button addSubview:label];
    
    UIImageView *_ico= [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-30, (44-6)/2, 10, 6)];
    
    if ([stateArray[section] isEqualToString:@"0"]) {
        _ico.image = [UIImage imageNamed:@"ico_listup"];
    }else if ([stateArray[section] isEqualToString:@"1"]){
        _ico.image = [UIImage imageNamed:@"ico_listdown"];
    }
    [button addSubview:_ico];
    return button;
}

//返回section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kCell_Height;
}

- (void)sectionbuttonClick:(UIButton *)sender{
    
    if ([stateArray[sender.tag-1]isEqualToString:@"1"]) {
        
        [stateArray replaceObjectAtIndex:sender.tag-1 withObject:@"0"];
    }else{
        [stateArray replaceObjectAtIndex:sender.tag-1 withObject:@"1"];
        
    }
    [TableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
}


@end
